﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace Model
{
    public class Model_MakePages
    {
        public Model_MakePages(string tblName, string strWhere, int pageIndex, int pageSize = 0)
        {
            this.tblName = tblName;
            fldNames = "*";
            keyFldName = tblName + "Id";
            selectOrderFldName = "";
            orderFldDesc = keyFldName + " DESC";
            this.strWhere = "1=1" + strWhere;
            PageIndex = pageIndex;

            if (pageSize == 0)
                PageSize = 10;//20
            else
                PageSize = pageSize;
        }

        private string _tblName;
        /// <summary>
        /// 要查询数据的表名
        /// </summary>
        public string tblName
        {
            set { _tblName = value; }
            get { return _tblName; }
        }

        private string _fldNames;
        /// <summary>
        /// 选择的字段列表以,分隔
        /// </summary>
        public string fldNames
        {
            set { _fldNames = value; }
            get { return _fldNames; }
        }

        private string _selectOrderFldName;
        /// <summary>
        /// 排序字段以,分隔（不能含keyFldName 指定的字段，可为空）
        /// </summary>
        public string selectOrderFldName
        {
            set { _selectOrderFldName = value; }
            get { return _selectOrderFldName; }
        }

        private string _orderFldDesc;
        /// <summary>
        /// 排序字段及排序方向，如addDate desc,id desc（排序字段需通过selectOrderFldName指定，可为空）
        /// </summary>
        public string orderFldDesc
        {
            set { _orderFldDesc = value; }
            get { return _orderFldDesc; }
        }

        private string _keyFldName;
        /// <summary>
        /// 主键字段
        /// </summary>
        public string keyFldName
        {
            set { _keyFldName = value; }
            get { return _keyFldName; }
        }

        private int _PageSize;
        /// <summary>
        /// 页尺寸
        /// </summary>
        public int PageSize
        {
            set { _PageSize = value; }
            get { return _PageSize; }
        }

        private int _PageIndex;
        /// <summary>
        /// 当前页
        /// </summary>
        public int PageIndex
        {
            set { _PageIndex = value; }
            get { return _PageIndex; }
        }

        private string _strWhere;
        /// <summary>
        /// 查询条件(注意: 不要加where) 
        /// </summary>
        public string strWhere
        {
            set { _strWhere = value; }
            get { return _strWhere; }
        }
    }

    ///// <summary>
    ///// 分页列表
    /////             作者：袁登明
    /////             日期：2014-5-23
    ///// 
    ///// </summary>
    //[DataContract]
    //[Serializable]
    //public class PageTable
    //{
    //    /// <summary>
    //    /// 当前页内容
    //    /// 
    //    /// </summary>
    //    [DataMember]
    //    public DataTable DataTable { get; set; }

    //    /// <summary>
    //    /// 当前页
    //    /// 
    //    /// </summary>
    //    [DataMember]
    //    public int PageIndex { get; set; }

    //    /// <summary>
    //    /// 每页记录数
    //    /// 
    //    /// </summary>
    //    [DataMember]
    //    public int PageSize { get; set; }

    //    /// <summary>
    //    /// 总记录数
    //    /// 
    //    /// </summary>
    //    [DataMember]
    //    public long TotalCount { get; set; }

    //    /// <summary>
    //    /// 总页数
    //    /// 
    //    /// </summary>
    //    [DataMember]
    //    public int TotalPages { get; set; }

    //    /// <summary>
    //    /// 是否有上一页
    //    /// 
    //    /// </summary>
    //    //public bool HasPreviousPage
    //    //{
    //    //    get
    //    //    {
    //    //        return this.PageIndex > 0;
    //    //    }
    //    //}
    //    public bool HasPreviousPage { get; set; }

    //    /// <summary>
    //    /// 是否有下一页
    //    /// 
    //    /// </summary>
    //    //public bool HasNextPage
    //    //{
    //    //    get
    //    //    {
    //    //        return this.PageIndex + 1 < this.TotalPages;
    //    //    }
    //    //}
    //    public bool HasNextPage { get; set; }

    //    /// <summary>
    //    /// 构造函数
    //    /// 
    //    /// </summary>
    //    public PageTable()
    //    {
    //    }

    //    /// <summary>
    //    /// 构造函数
    //    /// 
    //    /// </summary>
    //    /// <param name="source">数据源</param><param name="pageIndex">当前页</param><param name="pageSize">每页记录数</param>
    //    public PageTable(DataTable dt, int pageIndex, int pageSize)
    //    {
    //        this.TotalCount = dt == null ? 0L : (long)dt.Rows.Count;
    //        this.TotalPages = (int)this.TotalCount / pageSize;
    //        if (this.TotalCount % (long)pageSize > 0L)
    //            ++this.TotalPages;
    //        this.PageSize = pageSize;
    //        this.PageIndex = pageIndex;
    //        this.DataTable = dt;
    //    }

    //    /// <summary>
    //    /// 构造函数
    //    /// 
    //    /// </summary>
    //    /// <param name="source">数据源</param><param name="pageIndex">当前页</param><param name="pageSize">每页记录数</param><param name="totalCount">总记录数</param>
    //    public PageTable(DataTable dt, int pageIndex, int pageSize, long totalCount)
    //    {
    //        this.TotalCount = totalCount;
    //        this.TotalPages = (int)this.TotalCount / pageSize;
    //        if (this.TotalCount % (long)pageSize > 0L)
    //            ++this.TotalPages;
    //        this.PageSize = pageSize;
    //        this.PageIndex = pageIndex;
    //        this.DataTable = dt;

    //        HasPreviousPage = this.PageIndex > 0;
    //        HasNextPage = this.PageIndex + 1 < this.TotalPages;
    //    }
    //}

    /// <summary>
    /// 分页列表
    ///             作者：袁登明
    ///             日期：2014-5-23
    /// 
    /// </summary>
    public class PageTable
    {
        /// <summary>
        /// 当前页内容
        /// 
        /// </summary>
        public DataTable DataTable { get; set; }

        /// <summary>
        /// 当前页
        /// 
        /// </summary>
        public int PageIndex { get; set; }

        /// <summary>
        /// 每页记录数
        /// 
        /// </summary>
        public int PageSize { get; set; }

        /// <summary>
        /// 总记录数
        /// 
        /// </summary>
        public long TotalCount { get; set; }

        /// <summary>
        /// 总页数
        /// 
        /// </summary>
        public int TotalPages { get; set; }

        /// <summary>
        /// 是否有上一页
        /// 
        /// </summary>
        public bool HasPreviousPage { get; set; }

        /// <summary>
        /// 是否有下一页
        /// 
        /// </summary>
        public bool HasNextPage { get; set; }

        /// <summary>
        /// 构造函数
        /// 
        /// </summary>
        public PageTable()
        {
        }

        /// <summary>
        /// 构造函数
        /// 
        /// </summary>
        /// <param name="source">数据源</param><param name="pageIndex">当前页</param><param name="pageSize">每页记录数</param>
        public PageTable(DataTable dt, int pageIndex, int pageSize)
        {
            this.TotalCount = dt == null ? 0L : (long)dt.Rows.Count;
            this.TotalPages = (int)this.TotalCount / pageSize;
            if (this.TotalCount % (long)pageSize > 0L)
                ++this.TotalPages;
            this.PageSize = pageSize;
            this.PageIndex = pageIndex;
            this.DataTable = dt;
        }

        /// <summary>
        /// 构造函数
        /// 
        /// </summary>
        /// <param name="source">数据源</param><param name="pageIndex">当前页</param><param name="pageSize">每页记录数</param><param name="totalCount">总记录数</param>
        public PageTable(DataTable dt, int pageIndex, int pageSize, long totalCount)
        {
            this.TotalCount = totalCount;
            this.TotalPages = (int)this.TotalCount / pageSize;
            if (this.TotalCount % (long)pageSize > 0L)
                ++this.TotalPages;
            this.PageSize = pageSize;
            this.PageIndex = pageIndex;
            this.DataTable = dt;

            HasPreviousPage = this.PageIndex > 1;
            HasNextPage = this.PageIndex < this.TotalPages;
        }
    }
}
